library(survey)
library(foreign)
setwd("~/Replication Code/")

##---Loading in data ----
anes9297 <- read.dta("./data/anes9297_subset.dta")

ccap08 <- read.dta("./data/ccap08_subset.dta")

ccap12 <- read.dta("./data/ccap12_subset.dta")

vsg.dat <- read.dta("./data/vsg_subset.dta")

ccap16 <- read.dta("./data/ccap16_subset.dta")

# ANES 9294 -----
party <- -1 # toggle between -1 and 1 for Democrats and Republicans, respectively
x <- subset(anes9297, pid92_3 == party 
            & !is.na(rr94_scaled) & !is.na(rr92_scaled))
psych::alpha(cbind(x$rr_irish_92, x$rr_blklsds_92, x$rr_tryhrd_92, x$rr_slvry_92))

d <- svydesign(~1, data = x, weights = ~V940005)
svymean(~rr92_scaled, d, na.rm = T)
sqrt(svyvar(~rr92_scaled, d, na.rm = T))

x <- subset(anes9297, pid94_3 == party 
            & !is.na(rr94_scaled) & !is.na(rr92_scaled))
psych::alpha(cbind(x$rr_irish_94, x$rr_blklsds_94, x$rr_tryhrd_94, x$rr_slvry_94))

d <- svydesign(~1, data = x, weights = ~V940005)
svymean(~rr94_scaled, d, na.rm = T)
sqrt(svyvar(~rr94_scaled, d, na.rm = T))

# CCAP 08 -----
party <- -1 # toggle between -1 and 1 for Democrats and Republicans, respectively
x <- subset(ccap08, pid3_m == party 
            & !is.na(rr_m_scaled) & !is.na(rr_o_scaled))
psych::alpha(cbind(x$M_specfav, x$M_deserve, x$M_tryhard, x$M_pdisc))

d <- svydesign(~1, data = x, weights = ~weight)
svymean(~rr_m_scaled, d, na.rm = T)
sqrt(svyvar(~rr_m_scaled, d, na.rm = T))

x <- subset(ccap08, pid3_o == party
            & !is.na(rr_m_scaled) & !is.na(rr_o_scaled))
psych::alpha(cbind(x$O_specfav, x$O_deserve, x$O_tryhard, x$O_pdisc))

d <- svydesign(~1, data = x, weights = ~weight)
svymean(~rr_o_scaled, d, na.rm = T)
sqrt(svyvar(~rr_o_scaled, d, na.rm = T))

## CCAP 2012 -----
# Baseline
party <- -1 # toggle between -1 and 1 for Democrats and Republicans, respectively
x <- subset(ccap12, (p2 == 1 | p3 == 1) & pp_pid3_new == party 
            & !is.na(rr_p1_scaled) & (!is.na(rr_p2_scaled) | !is.na(rr_p3_scaled)))

psych::alpha(cbind(x$pp_deserve, x$pp_specfav, x$pp_tryhard, x$pp_pdisc))
d <- svydesign(~1, data = x, weights = ~weight)
svymean(~rr_p1_scaled, d, na.rm = T)
sqrt(svyvar(~rr_p1_scaled, d, na.rm = T))

# March
x <- subset(ccap12, p2 == 1  & pid3_new == party 
            & !is.na(rr_p1_scaled) & !is.na(rr_p2_scaled))
psych::alpha(cbind(x$deserve, x$specfav, x$tryhard, x$pdisc))

d <- svydesign(~1, data = x, weights = ~weight)
svymean(~rr_p2_scaled, d, na.rm = T)
sqrt(svyvar(~rr_p2_scaled, d, na.rm = T))

# August
x <- subset(ccap12, p3 == 1 & pid3_new == party
            & !is.na(rr_p1_scaled) & !is.na(rr_p3_scaled))
psych::alpha(cbind(x$deserve, x$specfav, x$tryhard, x$pdisc))

d <- svydesign(~1, data = x, weights = ~weight)
svymean(~rr_p3_scaled, d, na.rm = T)
sqrt(svyvar(~rr_p3_scaled, d, na.rm = T))

# VOTER Survey -----
party <- -1 # toggle between -1 and 1 for Democrats and Republicans, respectively
x <- subset(vsg.dat, pid3_12 == party 
            & !is.na(rr_sc_12) & !is.na(rr_sc_16))
psych::alpha(cbind(x$rr_deserve_12, x$rr_specfav_12, x$rr_pdisc_12, x$rr_thard_12))

d <- svydesign(~1, data = x, weights = ~weight)
svymean(~rr_sc_12, d, na.rm = T)
sqrt(svyvar(~rr_sc_12, d, na.rm = T))

x <- subset(vsg.dat, pid3_16 == party 
            & !is.na(rr_sc_12) & !is.na(rr_sc_16))
psych::alpha(cbind(x$rr_deserve_16, x$rr_specfav_16, x$rr_pdisc_16, x$rr_thard_16))

d <- svydesign(~1, data = x, weights = ~weight)
svymean(~rr_sc_16, d, na.rm = T)
sqrt(svyvar(~rr_sc_16, d, na.rm = T))

# CCAP 16 -----
party <- 1 # toggle between -1 and 1 for Democrats and Republicans, respectively
x <- subset(ccap16, b_pid3 == party 
            & !is.na(b_rr_sc) & !is.na(p_rr_sc))
psych::alpha(cbind(x$b_pdisc, x$b_ldes, x$b_thard))

d <- svydesign(~1, data = x, weights = ~weight_post)
svymean(~b_rr_sc, d, na.rm = T)
sqrt(svyvar(~b_rr_sc, d, na.rm = T))

x <- subset(ccap16, p_pid3 == party
            & !is.na(b_rr_sc) & !is.na(p_rr_sc))
psych::alpha(cbind(x$p_pdisc, x$p_ldes, x$p_thard))

d <- svydesign(~1, data = x, weights = ~weight_post)
svymean(~p_rr_sc, d, na.rm = T)
sqrt(svyvar(~p_rr_sc, d, na.rm = T))

